//
//  TransferTypeViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/9.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "TransferTypeViewController.h"
#import "TransferViewController.h"
#import "HMScannerViewController.h"

@interface TransferTypeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView*tableView;
@property (nonatomic,strong) FlatButton*directlyBtn;
@property (nonatomic,strong) FlatButton*sweepBtn;
@property (nonatomic,strong) NSArray*data;

@end

@implementation TransferTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self action];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.data = [[XHDataBaseManager shareInstance]getHistoryReceivesWallet:CURRENT_WALLET.wallet_name];
    [self.tableView reloadData];
}

- (void)action{
    kWeakSelf(self);
    [self.directlyBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.navigationController pushViewController:[TransferViewController new] animated:YES];
    }];
    
    [self.sweepBtn addTapBlock:^(UIButton * _Nonnull btn) {
        HMScannerViewController *scanner = [[HMScannerViewController alloc] initWithCardName:nil avatar:nil completion:^(NSString *stringValue) {
            if (![RegularExp validateEosAccountName:stringValue]) {
                [weakself.view makeToast:XLocalized(@"无效的账号！")];
                return;
            }
            TransferViewController *vc = [TransferViewController new];
            vc.account = stringValue;
            [weakself.navigationController pushViewController:vc animated:YES];
        }];
        [weakself.navigationController pushViewController:scanner animated:YES];
    }];
}

- (void)setupUI{
    self.title = XLocalized(@"转账");
    UIView*directlyBack = [UIView new];
    directlyBack.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:directlyBack];
    [directlyBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.customNavigationBar.mas_bottom).offset(AdaptedWidth(8));
        make.left.mas_equalTo(kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(140));
    }];
    [directlyBack addSubview:self.directlyBtn];
    [self.directlyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    directlyBack.layer.cornerRadius = AdaptedWidth(7);
    [directlyBack.layer setLayerShadow:kShadowColor offset:CGSizeMake(0, 0) radius:6];
    
    
    UIView*sweepBack = [UIView new];
    sweepBack.backgroundColor = UIColor.whiteColor;

    [self.view addSubview:sweepBack];
    [sweepBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.customNavigationBar.mas_bottom).offset(AdaptedWidth(8));
        make.left.equalTo(self.directlyBtn.mas_right).offset(AdaptedWidth(20));
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(140));
        make.width.equalTo(self.directlyBtn.mas_width);
    }];
    sweepBack.layer.cornerRadius = AdaptedWidth(7);
    [sweepBack.layer setLayerShadow:kShadowColor offset:CGSizeMake(0, 0) radius:6];
    [sweepBack addSubview:self.sweepBtn];
    [self.sweepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self.directlyBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:AdaptedWidth(20)];
    [self.sweepBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:AdaptedWidth(20)];
    
    UIView*tableBack = [UIView new];
    tableBack.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:tableBack];
    [tableBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sweepBack.mas_bottom).offset(AdaptedWidth(27));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.bottom.mas_equalTo(-kNormalSpace);
    }];
    tableBack.layer.cornerRadius = AdaptedWidth(7);
    self.tableView.layer.cornerRadius = AdaptedWidth(7);
    [tableBack.layer setLayerShadow:kShadowColor offset:CGSizeMake(0, 0) radius:6];
    [tableBack addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.textColor = UIColorHex(#B5B5B5);
        cell.detailTextLabel.font = kDefaultFont(12);
        cell.textLabel.font = kBoldFont(16);
        cell.textLabel.textColor = UIColorHex(#000000);
    }
    Receive *receive = [self.data objectOrNilAtIndex:indexPath.row];
    if (receive) {
        cell.textLabel.text = receive.wallet_name;
        cell.detailTextLabel.text = receive.addTime;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Receive *receive = [self.data objectOrNilAtIndex:indexPath.row];
    TransferViewController *vc = [TransferViewController new];
    vc.account = receive.wallet_name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.estimatedRowHeight=0;
        _tableView.estimatedSectionFooterHeight=0;
        _tableView.estimatedSectionHeaderHeight=0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        UIView*headV = [UIView new];
        headV.frame = CGRectMake(0, 0, 0, AdaptedWidth(50));
        UILabel *tipLab = [[UILabel alloc]initWithFrame:CGRectMake(kNormalSpace, 0, 200, headV.height)];
        [headV addSubview:tipLab];
        tipLab.text = XLocalized(@"最近转账");
        tipLab.font = kDefaultFont(16);
        _tableView .tableHeaderView = headV;
        _tableView .tableFooterView = [UIView new];
    }
    return _tableView;
}
- (FlatButton*)directlyBtn{
    if (!_directlyBtn) {
        _directlyBtn = [FlatButton button];
        [_directlyBtn setTitle:XLocalized(@"直接转账") forState:UIControlStateNormal];
        [_directlyBtn setTitleColor:UIColorHex(#000000) forState:UIControlStateNormal];
        _directlyBtn.titleLabel.font = kDefaultFont(13);
        [_directlyBtn setImage:IMAGE_NAMED(@"transfer") forState:UIControlStateNormal];
    }
    return _directlyBtn;
}
- (FlatButton*)sweepBtn{
    if (!_sweepBtn) {
        _sweepBtn = [FlatButton button];
        [_sweepBtn setTitle:XLocalized(@"扫码转账") forState:UIControlStateNormal];
        [_sweepBtn setTitleColor:UIColorHex(#000000) forState:UIControlStateNormal];
        _sweepBtn.titleLabel.font = kDefaultFont(13);
        [_sweepBtn setImage:IMAGE_NAMED(@"sweep") forState:UIControlStateNormal];
    }
    return _sweepBtn;
}
@end
